/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.util;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author edilson
 */
public class ValidatorSM implements Validator {

    public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void validateEmail(FacesContext context, UIComponent toValidate,
            Object value) {
        String email = (String) value;
        int parte1 = email.indexOf("@");
        int parte2 = email.indexOf(".", parte1);
        int parte3 = email.length();

        if (!(parte1 >= 1 && parte2 >= 5 && parte3 >= 8 && parte1 + 1 < parte2)) {
            ((UIInput) toValidate).setValid(false);

            FacesMessage message = new FacesMessage("E-mail inválido");
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
}
